package cn.otrue.patienthealthmanager.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.HealthApplication;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.utils.MyConst;
import cn.otrue.patienthealthmanager.view.StatisticsPopupWindow;

/**
 * 水平ListView的适配器
 * 
 * @author G
 * 
 */
public class HorizontalAdapter extends BaseAdapter {
	private List<Map<String, Object>> data;
	private StatisticsPopupWindow popupWindow;
	private LinearLayout ll;
	private Context context;
	private boolean isSugarDay;//是否是单天的血糖数据：用于统计:对此处基本没有用处
	private boolean isplus_heart_Day;//是否是单天的血压心率数据
	public HorizontalAdapter(Context context, List<Map<String, Object>> data,StatisticsPopupWindow popupWindow, LinearLayout ll,boolean isSugarDay,boolean isplus_heart_Day) {
		this.context = context;
		this.data = data;
		this.popupWindow = popupWindow;
		this.ll = ll;
		this.isSugarDay=isSugarDay;
		this.isplus_heart_Day=isplus_heart_Day;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		Map<String, Object> eachMap = data.get(position);
		View retval = LayoutInflater.from(HealthApplication.getInstance())
				.inflate(R.layout.item_body_state_chart, null);
		TextView tv_catt_state = (TextView) retval
				.findViewById(R.id.tv_catt_state);
		TextView tv_catt_dates = (TextView) retval
				.findViewById(R.id.tv_catt_dates);

		String diseasetime = eachMap.get("diseasetime").toString();
		final List<String> list = (List<String>) eachMap.get("diseaselist");

		if (list.size() == 1 && list.get(0).equals("正常")) {

			tv_catt_state.setTextColor(Color.parseColor("#15293d"));
			tv_catt_state.setText(list.get(0));
		} else {
			tv_catt_state.setTextColor(Color.parseColor("#ac371f"));
			tv_catt_state.setText("异常");
			retval.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					String[] dataArr = (String[]) list.toArray(new String[list.size()]);
					popupWindow = new StatisticsPopupWindow(context,MyConst.OTHER_INFO, dataArr,isSugarDay,isplus_heart_Day);

					popupWindow.showAtLocation(ll, Gravity.CENTER, 0, 0);
				}
			});

		}
		tv_catt_dates.setText(diseasetime.substring(5));

		return retval;
	}
}
